<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class JwtKeyGenerate extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('jwt:keygen')
            ->setDescription('Generate JWT secret key');
    }

    protected function execute(Input $input, Output $output)
    {
        $key = bin2hex(random_bytes(32)); // 生成256位密钥
        $output->writeln('Generated JWT Secret:');
        $output->writeln('<info>' . $key . '</info>');

        // 可选：自动写入.env文件
        if (file_exists('.env')) {
            file_put_contents('.env', "\nJWT_SECRET=$key\n", FILE_APPEND);
            $output->writeln('Key has been saved to .env');
        }
    }
}
